package smartclass.dao;

import java.util.ArrayList;

import smartclass.data.ManagerDataBaseHelper;
import smartclass.data.TableItem;
import smartclass.entity.StudentInfo;
import smartclass.present.LogInActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class StudentDataDao {

	static StudentDataDao studentDao;
	ManagerDataBaseHelper db;
	String tableName;

	public StudentDataDao() {
		// TODO Auto-generated constructor stub
		db = LogInActivity.db;
		tableName = ManagerDataBaseHelper.STUDENT_TABLE;
	}

	public static StudentDataDao getInstance() {
		if (studentDao == null) {
			studentDao = new StudentDataDao();
		}
		return studentDao;
	}

	// Add Item
	public boolean addStudent(StudentInfo student) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");

		ContentValues value = new ContentValues();
		value.put(TableItem.ID, student.getId());
		value.put(TableItem.NAME, student.getName());
		value.put(TableItem.USER_NAME, student.getUserName());
		value.put(TableItem.CLASS_NAME, student.getClassName());
		value.put(TableItem.ADDRESS, student.getAddress());
		value.put(TableItem.GENDER, student.getGender());
		value.put(TableItem.PASS_WORD, student.getPassword());
		value.put(TableItem.PHONE_NUMBER, student.getPhoneNumber());
		data.insert(tableName, null, value);
		data.close();
		return true;
	}

	// Get Item
	public StudentInfo getStudent(int id) {
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		StudentInfo studentInfo = new StudentInfo();
		Cursor c = null;

		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ TableItem.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			studentInfo.setId(Integer.parseInt(c.getString(0)));
			studentInfo.setName(c.getString(1));
			studentInfo.setUserName(c.getString(2));
			studentInfo.setClassName(c.getString(3));
			studentInfo.setAddress(c.getString(4));
			studentInfo.setGender(c.getString(5));
			studentInfo.setPassword(c.getString(6));
			studentInfo.setPhoneNumber(c.getString(7));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return studentInfo;
	}

	// Get All Item
	public ArrayList<StudentInfo> getAllStudent() {
		ArrayList<StudentInfo> allStudent = new ArrayList<StudentInfo>();
		Cursor c = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		String sql = "SELECT * FROM " + tableName;
		c = db.getReadableDatabase().rawQuery(sql, null);
		StudentInfo studentInfo = null;
		if (c.moveToFirst()) {
			do {
				studentInfo = new StudentInfo();
				studentInfo.setId(Integer.parseInt(c.getString(0)));
				studentInfo.setName(c.getString(1));
				studentInfo.setUserName(c.getString(2));
				studentInfo.setClassName(c.getString(3));
				studentInfo.setAddress(c.getString(4));
				studentInfo.setGender(c.getString(5));
				studentInfo.setPassword(c.getString(6));
				studentInfo.setPhoneNumber(c.getString(7));
				allStudent.add(studentInfo);
			} while (c.moveToNext());
		}
		return allStudent;
	}

	// Get password by id
	public String getPassword(int id) {
		String password = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		Cursor c = null;
		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ TableItem.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			password = c.getString(6);
			System.out.println(c.getInt(0)+" ");
		} catch (Exception e) {

		}
		return password;
	}

	// Edit Item

	public boolean updateStudent(StudentInfo student) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		ContentValues value = new ContentValues();
		value.put(TableItem.ID, student.getId());
		value.put(TableItem.NAME, student.getName());
		value.put(TableItem.USER_NAME, student.getUserName());
		value.put(TableItem.CLASS_NAME, student.getClassName());
		value.put(TableItem.ADDRESS, student.getAddress());
		value.put(TableItem.GENDER, student.getGender());
		value.put(TableItem.PASS_WORD, student.getPassword());
		value.put(TableItem.PHONE_NUMBER, student.getPhoneNumber());

		data.update(tableName, value, TableItem.ID + "=?",
				new String[] { String.valueOf(student.getId()) });
		data.close();
		return true;
	}

	// Delete Item

	public boolean deleteDirection(int id) {
		String sql = "DELETE FROM " + tableName + " WHERE " + TableItem.ID
				+ "=" + id;
		db.getWritableDatabase().execSQL(sql);
		return true;
	}

}
